package com.atguigu.gmall.activity.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 商品库存同步的交换机与队列
 */
@Configuration
public class SeckillGoodsDelayRabbitConfig {

    /**
     * 延时交换机
     * @return
     */
    @Bean("seckillGoodsDelayExchange")
    public Exchange seckillGoodsDelayExchange(){
        return ExchangeBuilder.directExchange("seckill_goods_delay_exchange").build();
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean("seckillGoodsDeadExchange")
    public Exchange seckillGoodsDeadExchange(){
        return ExchangeBuilder.directExchange("seckill_goods_dead_exchange").build();
    }

    /**
     * 延时队列
     * @return
     */
    @Bean("seckillGoodsDelayQueue")
    public Queue seckillGoodsDelayQueue(){
        return QueueBuilder
                .durable("seckill_goods_delay_queue")
                .withArgument("x-dead-letter-exchange","seckill_goods_dead_exchange")
                .withArgument("x-dead-letter-routing-key","seckill.goods.dead")
                .build();
    }

    /**
     * 死信队列
     * @return
     */
    @Bean("seckillGoodsDeadQueue")
    public Queue seckillGoodsDeadQueue(){
        return QueueBuilder.durable("seckill_goods_dead_queue").build();
    }

    /**
     * 延时队列与延时交换绑定
     * @param seckillGoodsDelayExchange
     * @param seckillGoodsDelayQueue
     * @return
     */
    @Bean
    public Binding delayBinding(@Qualifier("seckillGoodsDelayExchange") Exchange seckillGoodsDelayExchange,
                           @Qualifier("seckillGoodsDelayQueue") Queue seckillGoodsDelayQueue){
        return BindingBuilder.bind(seckillGoodsDelayQueue).to(seckillGoodsDelayExchange).with("seckill.goods.delay").noargs();
    }

    /**
     * 死信队列与死信交换绑定
     * @param seckillGoodsDeadExchange
     * @param seckillGoodsDeadQueue
     * @return
     */
    @Bean
    public Binding deadBinding(@Qualifier("seckillGoodsDeadExchange") Exchange seckillGoodsDeadExchange,
                           @Qualifier("seckillGoodsDeadQueue") Queue seckillGoodsDeadQueue){
        return BindingBuilder.bind(seckillGoodsDeadQueue).to(seckillGoodsDeadExchange).with("seckill.goods.dead").noargs();
    }

}
